<template>
  <div>
    <!-- 视频区域 -->
    <div class="videos">
      <video src="@/assets/video/homepage.mp4" loop muted autoplay></video>
    </div>
    <div class="body">
      <div>
        <img class="svg" src="../assets/svg/iaaiscolor.svg" />
      </div>
      <!-- 登陆区域 -->
      <div class="Login">
        <el-form class="login-container">
          <h1 class="title">用户登陆</h1>
          <el-form-item label="登陆方式" prop="type">
            <el-radio-group v-model="loginForm.type">
              <el-radio :label="0">密码登陆</el-radio>
              <el-radio :label="1">验证码</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- 密码登陆 -->
          <el-form-item v-if="loginForm.type === 0">
            <el-input
              type="type"
              placeholder="手机账号/统一信用代码"
              v-model="loginForm.account"
              autocomplete="off"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item v-if="loginForm.type === 0">
            <el-input
              placeholder="密码"
              v-model="loginForm.password"
              autocomplete="off"
              prefix-icon="el-icon-lock"
              type="password"
            ></el-input>

            <el-form-item class="login1">
              <el-button type="primary" @click="Login()" style="width: 48%"
                >登陆</el-button
              >
              <el-button type="primary" @click="Register()" style="width: 48%"
                >注册</el-button
              >
            </el-form-item>
          </el-form-item>

          <!-- 验证码登陆 -->
          <el-form-item v-if="loginForm.type === 1">
            <el-input
              type="type"
              placeholder="手机账号"
              v-model="loginForm.account"
              autocomplete="off"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item v-if="loginForm.type === 1">
            <el-input
              v-model="loginForm.code"
              autocomplete="off"
              placeholder="验证码"
              prefix-icon="el-icon-phone"
            >
              <el-button
                slot="append"
                style="color: white; background-color: #1890ff"
                v-show="showTime"
                @click="sendEmail(loginForm.phone)"
                >发送验证码</el-button
              >
              <el-button
                slot="append"
                style="
                  color: white;
                  background-color: #1890ff;
                  margin-left: -20px;
                "
                v-show="!showTime"
                >{{ sendTime }}秒</el-button
              >
            </el-input>
            <el-form-item class="login1">
              <el-button type="primary" @click="Logincode()" style="width: 48%"
                >登陆</el-button
              >
              <el-button type="primary" @click="Register()" style="width: 48%"
                >注册</el-button
              >
            </el-form-item>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login, putCode, loginBycode } from "@/http/api";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        type: 0,

        account: "",
        password: "",
        code: "",
      },
      code1: "",
      showTime: true /* 布尔值，通过v-show控制显示‘获取按钮’还是‘倒计时’ */,
      sendTime: "" /* 倒计时 计数器 */,
      timer: null,
    };
  },
  methods: {
    //账号登陆
    Login() {
      if (this.loginForm.account === "" || this.loginForm.account == null) {
        this.$message({
          duration: 1000,
          message: "移动智慧物联系统账号不能为空！",
          type: "false",
        });
        this.loginForm.account = "";
        this, (this.loginForm.password = "");
      } else if (
        this.loginForm.password === "" ||
        this.loginForm.password == null
      ) {
        this.$message({
          duration: 1000,
          message: "密码不能为空！",
          type: "false",
        });
        this.loginForm.account = "";
        this, (this.loginForm.password = "");
      } else {
        const data = {
          phone: this.loginForm.account,
          password: this.loginForm.password,
        };
        login(data).then((res) => {
          console.log(res);
          if (res.data.success == true) {
            localStorage.setItem("id", res.data.id);
            localStorage.setItem("controllerId", res.data.id);
            localStorage.setItem("identity", 0);
            this.$router.push({ path: "/home" });
          } else {
            this.$message({
              duration: 1000,
              message: "账号或密码错误！",
              type: "false",
            });
          }
        });
      }
    },
    Logincode() {
      if (this.loginForm.account === "" || this.loginForm.account == null) {
        this.$message({
          duration: 1000,
          message: "手机号不能为空！",
          type: "false",
        });
        this.loginForm.account = "";
        this, (this.loginForm.password = "");
      } else if (this.loginForm.code != this.code1) {
        this.$message({
          duration: 1000,
          message: "验证码错误！",
          type: "false",
        });
      } else {
        const data = {
          phone: this.loginForm.account,
        };
        loginBycode(data).then((res) => {
          if (res.data.success == true) {
            localStorage.setItem("id", res.data.id);
            localStorage.setItem("controllerId", res.data.id);
            localStorage.setItem("identity", 0);
            this.$router.push({ path: "/home" });
          } else {
            this.$message({
              duration: 1000,
              message: "账号或密码错误！",
              type: "false",
            });
          }
        });
      }
    },
    Register() {
      this.$router.push("/register");
    },
    sendEmail() {
      if (this.loginForm.account === "" || this.loginForm.account == null) {
        this.$message({
          duration: 1000,
          message: "手机号不能为空！",
          type: "false",
        });
        this.loginForm.account = "";
        this, (this.loginForm.password = "");
      } else {
        const TIME_COUNT = 60; //  更改倒计时时间
        if (!this.timer) {
          this.sendTime = TIME_COUNT;
          this.showTime = false;
          this.timer = setInterval(() => {
            if (this.sendTime > 0 && this.sendTime <= TIME_COUNT) {
              this.sendTime--;
            } else {
              this.showTime = true;
              clearInterval(this.timer); // 清除定时器
              this.timer = null;
            }
          }, 1000);
          const data = {
            phone: this.loginForm.account,
          };
          putCode(data).then((res) => {
            console.log(res);
            this.code1 = res.data;
          });
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login1 {
  margin-top: 20px;
}
.svg {
  width: 400px;
  opacity: 0.7;
}
.body {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 800px;
}

.videos {
  background-color: rgba(163, 169, 169, 0.8);
  position: absolute;
  width: 100%;
  height: 100%;

  video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.7;
  }
}

.login-container {
  border-radius: 10px;
  margin: 0 auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: rgb(251, 251, 251);
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  opacity: 0.9;
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: rgba(0, 0, 0, 0.9);
  opacity: 0.9;
}
</style>